package com.yixinsoft.basic.service.manage.system;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yixinsoft.basic.mapper.manage.system.RoleMapper;
import com.yixinsoft.basic.model.manage.system.Role;
import com.yixinsoft.basic.service.base.BaseService;

/**
 * Created by hqy on 2016/06/05
 */
@Service
public class RoleService extends BaseService<Role> {
	
	@Autowired
	private ResourceService resourceService;
	
	@Autowired
	private RoleMapper roleMapper;
	
	public List<Role> queryList(Role role) {
		return roleMapper.queryList(role);
	}
	
	public PageInfo<Role> queryListPage(Role role, Integer page, Integer rows) {
		PageHelper.startPage(page, rows);
		List<Role> list = roleMapper.queryListPage(role);
		return new PageInfo<>(list);
	}
	
	/**
     * 根据角色编号得到角色标识符列表
     * @param roleIds
     * @return
     */
    public Set<String> findRoles(Long... roleIds) {
    	Set<String> roles = new HashSet<>();
    	for (Long roleId : roleIds) {
    		Role role = queryById(roleId);
    		if (role != null) {
    			roles.add(role.getRoleName());
    		}
    	}
    	return roles;
    }
    
    /**
     * 根据角色编号得到权限字符串列表
     * @param roleIds
     * @return
     */
    public Set<String> findPermissions(Long[] roleIds) {
    	Set<Long> resourceIds = new HashSet<>();
    	for (Long roleId : roleIds) {
    		Role role = queryById(roleId);
    		if (role != null) {
    			resourceIds.addAll(role.getResources());
    		}
    	}
    	return resourceService.findPermissions(resourceIds);
    }
}
